package net.suncaper.mvc.service.impl;

import net.suncaper.mvc.mapper.ReviewReplyMapper;
import net.suncaper.mvc.mapper.impl.ReviewReplyMapperImpl;
import net.suncaper.mvc.model.domain.Message;
import net.suncaper.mvc.model.domain.Review;
import net.suncaper.mvc.model.domain.ReviewReply;
import net.suncaper.mvc.service.IMessageService;
import net.suncaper.mvc.service.IReviewReplyService;
import net.suncaper.mvc.service.IReviewService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 评论回复服务实现类
 */
public class ReviewReplyServiceImpl implements IReviewReplyService {
    private ReviewReplyMapper reviewReplyMapper = new ReviewReplyMapperImpl();
    private IReviewService reviewService = new ReviewServiceImpl();
    private IMessageService messageService = new MessageServiceImpl();

    /**
     * 添加评论回复
     * @param reviewReply 评论回复信息
     * @return 添加结果
     */
    @Override
    public boolean addReviewReply(ReviewReply reviewReply) {
        // 确保回复时间存在，如果不存在则设置为当前时间
        if (reviewReply.getReplyTime() == null) {
            reviewReply.setReplyTime(new Date());
        }
        
        boolean result = reviewReplyMapper.insertReviewReply(reviewReply);
        
        if (result) {
            // 获取评论
            Review review = reviewService.getReviewById(reviewReply.getReviewId());
            if (review != null) {
                // 发送消息通知给评论作者（如果回复者不是评论作者）
                if (!review.getUserId().equals(reviewReply.getUserId())) {
                    // 创建消息对象
                    Message message = new Message();
                    message.setUserId(review.getUserId());
                    message.setTitle("评论回复通知");
                    
                    // 如果评论内容过长，进行截断
                    String reviewContent = review.getContent();
                    if (reviewContent.length() > 20) {
                        reviewContent = reviewContent.substring(0, 20) + "...";
                    }
                    
                    message.setContent("您的评论\"" + reviewContent + "\"收到了回复：" + reviewReply.getContent());
                    message.setType("review_reply");
                    message.setRelatedId(reviewReply.getReviewId().toString());
                    message.setIsRead(false);
                    message.setCreateTime(new Date());
                    
                    // 保存消息
                    try {
                        messageService.addMessage(message);
                    } catch (Exception e) {
                        // 记录日志但不影响主流程
                        System.err.println("[ReviewReplyServiceImpl] 发送消息通知失败：" + e.getMessage());
                    }
                }
            }
        }
        
        return result;
    }
    
    /**
     * 根据回复ID获取评论回复
     * @param replyId 回复ID
     * @return 评论回复信息
     */
    @Override
    public ReviewReply getReviewReplyById(Long replyId) {
        return reviewReplyMapper.selectReviewReplyById(replyId);
    }
    
    /**
     * 根据评论ID获取所有回复列表
     * @param reviewId 评论ID
     * @return 评论回复列表
     */
    @Override
    public List<ReviewReply> getReviewRepliesByReviewId(Long reviewId) {
        return reviewReplyMapper.selectReviewRepliesByReviewId(reviewId);
    }
    
    /**
     * 根据用户ID获取所有回复列表
     * @param userId 用户ID
     * @return 评论回复列表
     */
    @Override
    public List<ReviewReply> getReviewRepliesByUserId(String userId) {
        return reviewReplyMapper.selectReviewRepliesByUserId(userId);
    }
    
    /**
     * 获取所有评论回复
     * @return 评论回复列表
     */
    @Override
    public List<ReviewReply> getAllReviewReplies() {
        return reviewReplyMapper.selectAllReviewReplies();
    }
    
    /**
     * 更新评论回复
     * @param reviewReply 评论回复信息
     * @return 更新结果
     */
    @Override
    public boolean updateReviewReply(ReviewReply reviewReply) {
        // 获取原来的回复
        ReviewReply existingReply = reviewReplyMapper.selectReviewReplyById(reviewReply.getReplyId());
        if (existingReply == null) {
            return false;
        }
        
        // 更新内容，但保持其他信息不变
        if (reviewReply.getContent() != null) {
            existingReply.setContent(reviewReply.getContent());
        }
        
        // 自动更新回复时间为当前时间
        existingReply.setReplyTime(new Date());
        
        return reviewReplyMapper.updateReviewReply(existingReply);
    }
    
    /**
     * 通过回复ID删除评论回复
     * @param replyId 回复ID
     * @return 删除结果
     */
    @Override
    public boolean deleteReviewReplyById(Long replyId) {
        return reviewReplyMapper.deleteReviewReplyById(replyId);
    }
    
    /**
     * 通过评论ID删除所有相关回复
     * @param reviewId 评论ID
     * @return 删除结果
     */
    @Override
    public boolean deleteReviewRepliesByReviewId(Long reviewId) {
        return reviewReplyMapper.deleteReviewRepliesByReviewId(reviewId);
    }
} 